/*
 * The MIT License (MIT)
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

use napi::bindgen_prelude::*;
use napi::JsString;
use napi_derive::napi;

#[napi]
pub fn env_create_string(env: Env) -> Result<JsString> {
  env.create_string("env crete string")
}

#[napi]
pub fn env_throw_error(env: Env) -> Result<i32> {
  env.throw_error("rust throw error", Some("napi error 1"))?;
  Ok(12)
}

#[napi]
pub fn env_throw_range_error(env: Env) -> Result<i32> {
  env.throw_range_error("rust throw range error", Some("napi error 2"))?;
  Ok(12)
}

#[napi]
pub fn env_throw_type_error(env: Env) -> Result<i32> {
  env.throw_type_error("rust throw type error", Some("napi error 3"))?;
  Ok(12)
}

#[napi]
pub fn env_throw(env: Env) -> Result<i32> {
  let err = env.create_error(Error::from_reason("rust throw"))?;
  env.throw(err)?;
  Ok(12)
}
